<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="事件" prop="event">
        <el-input
          v-model="queryParams.event"
          placeholder="请输入事件"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-s-tools"
          size="mini"
          @click="handleConfig"
          v-hasPermi="['tool:sms:edit']"
        >配置</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['tool:sms:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['tool:sms:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="smsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="短信ID" align="center" prop="smsId" />
      <el-table-column label="事件" align="center" prop="event" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="消息" align="center" prop="msg" />
      <el-table-column label="IP" align="center" prop="ip" />
      <el-table-column label="短信平台类型" align="center" prop="type" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['tool:sms:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['tool:sms:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改短信通讯对话框 -->
    <el-dialog :title="title" :visible.sync="configOpen" :close-on-click-modal="false" width="600px" @close="handleConfigCancel">
      <el-form ref="configForm" :model="configForm" :rules="configRules" label-width="150px">
        <el-form-item label="短信类型" prop="storageType">
          <el-radio-group v-model="configForm.smsType">
            <el-radio v-for="item in smsTypeList"
                      :key="item.value"
                      :label="item.value"
                      :value="item.value">{{item.name}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="accessKeyId" prop="accessKeyId" v-if="configForm.smsType != '0'">
          <el-input v-model="configForm.accessKeyId" placeholder="请输入accessKeyId" />
        </el-form-item>
        <el-form-item label="accessKeySecret" prop="accessKeySecret" v-if="configForm.smsType != '0'">
          <el-input type="password" v-model="configForm.accessKeySecret" placeholder="请输入accessKeySecret" />
        </el-form-item>

        <el-form-item label="签名名称" prop="signName" v-if="configForm.smsType != '0'">
          <el-input v-model="configForm.signName" placeholder="请输入签名名称" />
        </el-form-item>
        <el-form-item label="注册模版编号" prop="registerCode" v-if="configForm.smsType != '0'">
          <el-input v-model="configForm.registerCode" placeholder="请输入注册模版编号" />
        </el-form-item>
        <el-form-item label="登录验证码模版编号" prop="loginCode" v-if="configForm.smsType != '0'">
          <el-input v-model="configForm.loginCode" placeholder="请输入登录验证码模版编号" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleConfigSubmit">确 定</el-button>
        <el-button @click="handleConfigCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSms, getSms, delSms, addSms, updateSms, exportSms } from "@/api/tool/sms";
import { setConfigKey } from "@/api/system/config";
export default {
  name: "Sms",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 短信通讯表格数据
      smsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      configOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        event: undefined,
        phone: undefined,
        msg: undefined,
        ip: undefined,
        type: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      smsTypeList: [
        {value: '0', name: '无'},
        {value: '1', name: '阿里云短信平台'},
      ],
      configForm:{
        smsType:'',
        accessKeyId:'',
        accessKeySecret:'',
        signName:'',
        registerCode:'',
        forgetCode:'',
        verifyCode:'',
        updPhoneCode:'',
        loginCode:'',
      },
      configRules:{
        accessKeyId: [
          { required: true, message: "请输入accessKeyId", trigger: "blur" }
        ],
        accessKeySecret: [
          { required: true, message: "请输入accessKeySecret", trigger: "blur" }
        ],
        signName: [
          { required: true, message: "请输入签名名称", trigger: "blur" }
        ],
        registerCode: [
          { required: true, message: "请输入注册模版编号", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询短信通讯列表 */
    getList() {
      this.loading = true;
      listSms(this.queryParams).then(response => {
        this.smsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        smsId: undefined,
        event: undefined,
        phone: undefined,
        msg: undefined,
        ip: undefined,
        type: undefined,
        createTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.smsId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加短信通讯";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const smsId = row.smsId || this.ids
      getSms(smsId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改短信通讯";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.smsId != undefined) {
            updateSms(this.form).then(response => {
               this.msgSuccess("修改成功");
               this.open = false;
               this.getList();
            });
          } else {
            addSms(this.form).then(response => {
               this.msgSuccess("新增成功");
               this.open = false;
               this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const smsIds = row.smsId || this.ids;
      this.$confirm('是否确认删除短信通讯编号为"' + smsIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSms(smsIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有短信通讯数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportSms(queryParams);
        }).then(response => {
          this.download(response.data);
        }).catch(function() {});
    },
    handleConfigCancel(){
      this.configOpen = false;
    },
    handleConfig: function(){
      this.getConfigKey("sys.sms.config").then(response => {
        this.configForm = JSON.parse(response.data);
        this.configOpen = true;
      });
    },
    handleConfigSubmit: function(){
      this.$refs["configForm"].validate(valid => {
        if(valid){
          setConfigKey('sys.sms.config',JSON.stringify(this.configForm)).then(response => {
            this.msgSuccess("更新配置信息成功");
            this.configOpen = false;
          });
        }
      });
    },
  }
};
</script>
